package com.sunagy.demand.front.service.impl;

import com.sunagy.demand.front.dataobject.CustomerProgress;
import com.sunagy.demand.front.dataobject.DictType;
import com.sunagy.demand.front.dataobject.DictValue;
import com.sunagy.demand.front.dto.CustomerProgressDto;
import com.sunagy.demand.front.repository.CustomerProgressRepository;
import com.sunagy.demand.front.repository.DictTypeRepository;
import com.sunagy.demand.front.repository.DictValueRepository;
import com.sunagy.demand.front.service.CustomerProgressService;
import com.sunagy.demand.front.utils.BeanUtil;
import com.sunagy.demand.front.utils.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * desc：#TODO fill me ！
 * author：三餘無夢生
 * date：2018/4/26
 * time：上午11:44
 */
@Service
public class CustomerProgressServiceImpl implements CustomerProgressService {

    @Autowired
    private CustomerProgressRepository customerProgressRepository;

    @Autowired
    private DictTypeRepository dictTypeRepository;

    @Autowired
    private DictValueRepository dictValueRepository;

    @Override
    @Transactional
    public List<CustomerProgressDto> getEnableListByUserId(Integer userid) {
        List<CustomerProgress> customerProgressList = customerProgressRepository.findAllByEnableAndUserid(true,userid);
        if (customerProgressList.size() == 0){
            DictType dictType = dictTypeRepository.findByCode("demand_progress");
            Integer dictId = dictType.getId();
            List<DictValue> dictValueList = dictValueRepository.findByTypeId(dictId);
            for (DictValue dictValue : dictValueList){
                CustomerProgress customerProgress = new CustomerProgress();
                customerProgress.setDic_code(dictValue.getCode());
                customerProgress.setDic_name(dictValue.getLabel());
                customerProgress.setProgress_name(dictValue.getLabel());
                customerProgress.setUserid(UserContext.getUserID());
                customerProgress.setStatus(0);
                customerProgress.setEnable(true);
                customerProgress.setSort(1);
                customerProgressList.add(customerProgress);
                customerProgressList = customerProgressRepository.save(customerProgressList);
            }
        }
        List<CustomerProgressDto> customerProgressDtoList = (List<CustomerProgressDto>) BeanUtil.copyPropertiesList(customerProgressList,CustomerProgressDto.class);
        return customerProgressDtoList;
    }

    @Override
    @Transactional
    public void dealProgress(Integer id) {
        CustomerProgress customerProgress = customerProgressRepository.findOne(id);
        customerProgress.setStatus(1);
        customerProgressRepository.save(customerProgress);
    }
}
